def nr47(a):
n4 = a.count('4')
n7 = a.count('7')
pre = 0
if n4 < n7:
need = (n7 - n4) // 2
i = 1
k = 0
while k < need + 1 or (a[-i]) == '7':
k += (a[-i] == '7')
i += 1
a = a[:-i] + '7'
a += '4' * (need + i - k)
a += '7' * (k - need - 1)
elif n4 > n7:
need = (n4 - n7) // 2
i = 1
k = 0
while k < need:
k += a[-i] == '4'
i += 1
i -= 1
a = a[:-i] + '7' * i
return a
def n47(a):
ret = ''
g = len(a)
i = 0
for t in a:
if int(t) < 4:
ret += (g - i) * '4'
break
elif int(t) == 4:
ret += '4'
n4 = i
elif int(t) < 7:
ret += '7' + '4' * (g - i - 1)
break
elif int(t) == 7:
ret += '7'
else:
ret = ret[:n4] + '7' + '4' * (g - n4 - 1)
break
i += 1
return ret
def big(a):
global k
o = 1
for t in a:
if int(t) > 7 or (o > k // 2 and int(t) > 4):
return True
elif (int(t) < 7 and not (o > k // 2)) or (int(t) < 4 and (o > k // 2)):
return False
o += 1
return False
a = input()
k = len(a)
if k % 2 == 1:
print('4' * ((k + 1) // 2) + '7' * ((k + 1) // 2))
else:
if big(a):
print('4' * (k // 2 + 1) + '7' * (k // 2 + 1))
else:
a = nr47(n47(a))
print(a)
1582C - Grandma Capa Knits a Scarf | 492A - Vanya and Cubes |
217A - Ice Skating | 270A - Fancy Fence |
181A - Series of Crimes | 1638A - Reverse |
1654C - Alice and the Cake | 369A - Valera and Plates |
1626A - Equidistant Letters | 977D - Divide by three multiply by two |
1654B - Prefix Removals | 1654A - Maximum Cake Tastiness |
1649A - Game | 139A - Petr and Book |
1612A - Distance | 520A - Pangram |
124A - The number of positions | 1041A - Heist |
901A - Hashing Trees | 1283A - Minutes Before the New Year |
1654D - Potion Brewing Class | 1107B - Digital root |
25A - IQ test | 785A - Anton and Polyhedrons |
1542B - Plus and Multiply | 306A - Candies |
1651C - Fault-tolerant Network | 870A - Search for Pretty Integers |
1174A - Ehab Fails to Be Thanos | 1169A - Circle Metro |